package huzh.foundation.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *<p>title:DataSet</p>
 *<p>description:</p>
 * @since 2014-6-8
 * @author huzh
 * @version 1.0
 */
public class DataSet  implements Iterator<Object>, Serializable{
    /**serialVersionUID*/
    private static final long serialVersionUID = 52904247515934872L;
    /**resultMetaData*/
    private DAOResultMetaData resultMetaData;
    /**resultList*/
    private ArrayList<Object> resultList;
    /**totalRowNum*/
    private int totalRowNum;
    /**returnRowNum*/
    private int returnRowNum;
    /**currentRow*/
    private int currentRow;

    /**
     * @param resultMetaData
     * @param resultList
     * @param totalRowNum
     */
    public DataSet(DAOResultMetaData resultMetaData, ArrayList<Object> resultList, int totalRowNum){
        this.resultMetaData = resultMetaData;
        this.resultList = resultList;
        this.totalRowNum = totalRowNum;
        if (resultList != null)
            this.returnRowNum = resultList.size();
        else {
            this.returnRowNum = 0;
        }
        this.currentRow = -1;
    }

    /**
     * @return
     */
    public boolean hasNext(){
        return (this.returnRowNum > 0) && (this.currentRow < this.returnRowNum - 1);
    }

    /**
     * @return
     */
    public Object next(){
        this.currentRow += 1;
        return getDataRow(this.currentRow);
    }

    @SuppressWarnings("unchecked")
    public DataRow getDataRow(int rowId){
        return new DataRow(this.resultMetaData, (ArrayList)this.resultList.get(rowId), rowId);
    }

    /**
     * 
     */
    public void reset() {
        this.currentRow = -1;
    }

    /**
     * @return
     */
    public boolean isEmpty(){
        return this.returnRowNum == 0;
    }

    /**
     * 
     */
    public void remove() {
        throw new UnsupportedOperationException("Method remove() not yet implemented.");
    }

    /**
     * @return
     */
    public DAOResultMetaData getResultMetaData() {
        return this.resultMetaData;
    }
    /**
     * @param resultMetaData
     */
    public void setResultMetaData(DAOResultMetaData resultMetaData) {
        this.resultMetaData = resultMetaData;
    }
    /**
     * @return
     */
    public ArrayList<Object> getResultList() {
        return this.resultList;
    }
    /**
     * @param resultList
     */
    public void setResultList(ArrayList<Object> resultList) {
        this.resultList = resultList;
    }
    /**
     * @return
     */
    public int getTotalRowNum() {
        return this.totalRowNum;
    }
    /**
     * @param totalRowNum
     */
    public void setTotalRowNum(int totalRowNum) {
        this.totalRowNum = totalRowNum;
    }
    /**
     * @return
     */
    public int getReturnRowNum() {
        return this.returnRowNum;
    }
    /**
     * @param returnRowNum
     */
    public void setReturnRowNum(int returnRowNum) {
        this.returnRowNum = returnRowNum;
    }
    /**
     * @return
     */
    public int getCurrentRow() {
        return this.currentRow;
    }

    /**
     * @param colIndex
     * @param formula
     * @param decimalCount
     */
    public void computeColumn(int colIndex, String formula, int decimalCount){
        for (int i = 0; i < getResultList().size(); i++)
            getDataRow(i).computeColumn(colIndex, formula, decimalCount);
    }

    /**
     * @return
     */
    public String toString(){
        return "totalRowNum::" + this.totalRowNum + "\r\nDAOResultMetaData::" + this.resultMetaData 
            + "]\r\nresultList::" + this.resultList;
    }
}
